import { Map } from 'immutable'
import * as actionTypes from './actionTypes'
import { getLoginInfo } from '@/utils/secret-key'

const { token, profile, cookie } = getLoginInfo('loginInfo')

const defaultState = Map({
    isShow: false,
    isLogin: token ? true : false, // 登录状态
    profile: profile || {},
    token: token || '',
    cookie: cookie || '',
})

function reducer(state = defaultState, action) {
    switch (action.type) {
        case actionTypes.CHANGE_IS_VISIBLE_STATE:
            return state.set('isShow', action.isShow)
        case actionTypes.CHANGE_USER_LOGIN_STATE:
            return state.set('isLogin', action.isLogin)
        case actionTypes.CHANGE_PROFILE_INFO:
            return state.set('profile', action.profile)
        case actionTypes.CHANGE_PROFILE_TOKEN:
            return state.set('token', action.token)
        case actionTypes.CHANGE_PROFILE_COOKIE:
            return state.set('cookie', action.cookie)
        default:
            return state
    }
}

export default reducer